package cn.itcast.bos.web.base.action;

import domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/7/11/011.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Actions
@Scope("prototype")
public class CourierAction extends BaseAction<Courier,CourierService>{

    private Courier courier = new Courier();

    private String ids;
    private int page;
    private int rows;

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    @Override
    public Courier getModel() {
        return courier;
    }

    @Action(value = "courier_save",results = {
            @Result(name = "success",type = "redirect",location = "/pages/base/courier.html")})
    public String save(){
        service.save(courier);
        System.out.println("收派人数据保存...");
        return SUCCESS;
    }

    @Action(value = "courier_pageQuery",results = {
            @Result(name = "success",type = "json")
    })
    public String pageQuery(){

        Specification<Courier> specification = new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                //如果第一个查询条件不为空
                if (StringUtils.isNotBlank(courier.getCourierNum())){
                    //断言 表达的是Where 之后的语句,这里相当于是动态拼接SQL语句
                    //等于name = %?%,xxx
                    Predicate p1 = cb.equal(root.get("courierNum").as(String.class),courier.getCourierNum());
                    list.add(p1);
                }
                if(StringUtils.isNotBlank(courier.getCompany())){
                    //这里可以用like进行模糊查询
                    Predicate p2 = cb.like(root.get("company").as(String.class),'%'+courier.getCompany()+'%');
                    list.add(p2);
                }
                if(StringUtils.isNotBlank(courier.getType())){
                    //这里可以用like进行模糊查询
                    Predicate p3 = cb.like(root.get("type").as(String.class),'%'+courier.getType()+'%');
                    list.add(p3);
                }
                //多表查询
                Join<Object,Object> standardRoot = root.join("standard",JoinType.INNER);
                if(courier.getStandard()!=null&&StringUtils.isNotBlank(courier.getStandard().getName())){
                    Predicate p4 = cb.like(standardRoot.get("name").as(String.class),'%'+courier.getStandard().getName()+'%');
                    list.add(p4);
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };
        //封装page和row信息
        Pageable pageable = new PageRequest(page-1,rows);
        //通过继承JpaSpecification接口使其findAll方法获得带条件检索功能
        Page<Courier> pageData = service.findPageData(specification,pageable);

        Map<String,Object> result = new HashMap<>();
        result.put("total",pageData.getNumberOfElements());
        result.put("rows",pageData.getContent());
        ActionContext.getContext().getValueStack().push(result);

        return SUCCESS;
    }

    @Action(value = "courier_mark_deltag",results = {
            @Result(name = "success",type = "json")
    })
    public String markDelTag() {
        System.out.println(ids);
        String[] idsArray = ids.split(",");
        List<Integer> list = new ArrayList<>();
        for (String id: idsArray){
            list.add(Integer.valueOf(id));
        }
        service.markDelTag(list);
        return SUCCESS;
    }

    @Action(value = "courier_restore",results = {
            @Result(name = "success",type = "json")
    })
    public String restore(){
        String[] idsArray = ids.split(",");
        List<Integer> list = new ArrayList<>();
        for (String id: idsArray){
            list.add(Integer.valueOf(id));
        }
        service.restore(list);
        return SUCCESS;
    }

}
